<?php
  include_once('../conn.php');

  // 个人中心归还书籍更新
  $id = $_GET['id'];
  $sql = "select * from apply where id = $id";  // 获取状态信息
  $result = mysqli_query($conn, $sql);
  $row = mysqli_fetch_assoc($result);
  $apply_statue = $row['apply_statue'];
  // echo $apply_statue;
  if($apply_statue == 2 || $apply_statue == 0) {  // 判断是否需要归还
    alert('该书籍处于未通过状态，不需归还', 'me_book_list.php');
    exit();
  }
  $book_id = $row['book_id'];
  $sql_book_select = "select * from book where id = $book_id"; // 获取现有书籍数量
  $result_book_select = mysqli_query($conn, $sql_book_select);
  $row_book = mysqli_fetch_assoc($result_book_select);
  echo $book_count = $row_book['book_count'];
  echo $apply_num = $row['apply_num'];
  $book_count = $book_count + $apply_num; // 归还后书籍数量
  $sql_book_update = "update book set book_count = $book_count where id = $book_id";
  $result_book_update = mysqli_query($conn, $sql_book_update);  // 更新book数据表中书籍数量
  $sql_apply_update = "update apply set apply_statue = 3 where id = $id";
  $result_apply_update = mysqli_query($conn, $sql_apply_update);  // 更新apply数据表中书籍状态
  if($result_book_update && $result_apply_update) {
    alert('已归还，并恢复该书籍数量', 'me_book_list.php');
  } else {
    alert('操作失败', 'me_book_list.php');
  }
  mysqli_close($conn);
?>